//
//  HNCreditGoidJiaZhang.m
//  XDEducation
//
//  Created by 星道 on 17/6/1.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNCreditGoidJiaZhang.h"
#import "HNCreditGoldSelectView.h"
#import "HNCreditGoldController.h"
@implementation HNCreditGoidJiaZhang
{
    UIViewController *_VC;
    
    NSNumber *_ChildID;
    NSDictionary *_xx;
}

- (instancetype)init_GetJiaZhangInfoWithVC:(UIViewController *)VC
{
    self = [super init];
    if (self)
    {
        _VC = VC;
    }
    return self;
}



- (void)SelectRowWithTitle:(NSString *)Title
{
    
    if ([Title isEqualToString:@"老师"])
    {
        [self Teacher];
    }
    else if ([Title isEqualToString:@"自己"])
    {
        [self Own];
    }
    else if ([Title isEqualToString:@"孩子"])
    {
        [self Child];
    }
    else
    {
    }
}


#pragma mark - 自己
- (void)Own
{
    HNCreditGoldController *VC = [[HNCreditGoldController alloc] init];
    //1、首先获取用户头像接口

    VC.Image = [AccountTool shareAccountTool].currentAccount.headImageUrl;
    NSString *FamilyID = [AccountTool shareAccountTool].currentAccount.uid;
        //2、获取顶部数据
        [self RefreshDataWithUrlStr:@"user.do?action=getHonestValues" Params:@{@"uid":FamilyID} Complete:^(id responseObj) {
            VC.Love = responseObj[@"t"][@"redHearts"];
            VC.Glory = responseObj[@"t"][@"stars"];
            VC.Efforts = responseObj[@"t"][@"flowers"];
            VC.Contract = responseObj[@"t"][@"honests"];
            
            [self RefreshDataWithUrlStr:@"user.do?action=getTargetStatus2" Params:@{@"supervisorUid":FamilyID} Complete:^(id responseObj) {
                
                //小目标
                VC.Small_NoFinish = responseObj[@"t"][@"notFinished"];
                VC.Small_Finish = responseObj[@"t"][@"finished"];
                
                [self RefreshDataWithUrlStr:@"user.do?action=getBigTargetStatus2" Params:@{@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {

                    //大目标
                    VC.Big_NoFinish = responseObj[@"t"][@"notFinished"];
                    VC.Big_Finish = responseObj[@"t"][@"finished"];
                    VC.HNCreditGoldControllerAccoutType = HNCreditGoldControllerAccoutTypeZiJi;
                    VC.uid = [AccountTool shareAccountTool].currentAccount.uid;
                    [_VC.navigationController pushViewController:VC animated:YES];
                }];
            }];
            
        }];
}

#pragma mark - 孩子 
- (void)Child
{
    HNCreditGoldController *VC = [[HNCreditGoldController alloc] init];
    //1、首先获取用户头像接口

        [self GetChildIDWithComplete:^(NSNumber *ChildID,NSDictionary *xx) {
        
        NSDictionary *Obj = xx[@"t"];
        if (![Obj[@"avatar"] isKindOfClass:[NSNull class]])
        {
            VC.Image = (Obj[@"avatar"]);
        }
        NSString *FamilyID = [Obj[@"uid"] stringValue];
        //2、获取顶部数据
        [self RefreshDataWithUrlStr:@"user.do?action=getHonestValues" Params:@{@"uid":FamilyID} Complete:^(id responseObj) {
            VC.Love = responseObj[@"t"][@"redHearts"];
            VC.Glory = responseObj[@"t"][@"stars"];
            VC.Efforts = responseObj[@"t"][@"flowers"];
            VC.Contract = responseObj[@"t"][@"honests"];
            
            [self RefreshDataWithUrlStr:@"user.do?action=getTargetStatus2" Params:@{@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"executeUid":FamilyID} Complete:^(id responseObj) {
                
                //小目标
                VC.Small_NoFinish = responseObj[@"t"][@"notFinished"];
                VC.Small_Finish = responseObj[@"t"][@"finished"];
                
                [self RefreshDataWithUrlStr:@"user.do?action=getBigTargetStatus2" Params:@{@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"executeUid":FamilyID,@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
                    
                    //大目标
                    VC.Big_NoFinish = responseObj[@"t"][@"notFinished"];
                    VC.Big_Finish = responseObj[@"t"][@"finished"];
                    VC.HNCreditGoldControllerAccoutType = HNCreditGoldControllerAccoutTypeHaiZi;
                    VC.uid = FamilyID;
                    [_VC.navigationController pushViewController:VC animated:YES];
                }];
            }];
            
        }];
    }];
}


- (void)GetChildIDWithComplete:(void(^)(NSNumber *ChildID,NSDictionary *xx))Complete
{
    if (_ChildID && _xx)
    {
        Complete (_ChildID,_xx);
    }
    else
    {
        [self RefreshDataWithUrlStr:@"user.do?action=getMyChild" Params:@{@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id xx) {
            NSDictionary *Obj = xx[@"t"];
            _xx = xx;
            _ChildID = Obj[@"uid"];
            Complete (_ChildID,_xx);
        }];
    }

}

#pragma mark - 老师
- (void)Teacher
{
    //打开所有老师
    HNCreditGoldController *VC = [[HNCreditGoldController alloc] init];
    [self RefreshDataWithUrlStr:@"qun.do?action=getqun" Params:@{@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
        NSMutableArray *Arr =[NSMutableArray array];
        NSMutableArray *Title_Arr = [NSMutableArray array];
        for (NSDictionary *Dict in responseObj[@"qusers"])
        {
            if ([Dict[@"petName"] isKindOfClass:[NSNull class]])
            {
                continue;
            }
            
            if (![Dict[@"rolecode"] isEqualToString:@"2"])
            {
                continue;
            }
            
            
            [Arr addObject:Dict];
            NSString *PetName = Dict[@"petName"];
            [Title_Arr addObject:PetName];
        }
        
        HNCreditGoldSelectView *SelectView = [[HNCreditGoldSelectView alloc] initWithTitle_Arr:Title_Arr SelectRow:^(NSString *Title) {
            
            //获取次级老师的数据后打开我的约定
            for (NSDictionary *Dict in Arr)
            {
                if ([Dict[@"petName"] isEqualToString:Title])
                {
                    [self RefreshDataWithUrlStr:@"user.do?action=getHonestValues" Params:@{@"uid":Dict[@"uid"]} Complete:^(id responseObj) {
                        [self GetChildIDWithComplete:^(NSNumber *executeUid, NSDictionary *xx) {
                        //爱心荣耀努力合约
                        if (![Dict[@"avatar"] isKindOfClass:[NSNull class]])
                        {
                            VC.Image = Dict[@"avatar"];
                        }
                        VC.Love = responseObj[@"t"][@"redHearts"];
                        VC.Glory = responseObj[@"t"][@"stars"];
                        VC.Efforts = responseObj[@"t"][@"flowers"];
                        VC.Contract = responseObj[@"t"][@"honests"];
                        
                        [self RefreshDataWithUrlStr:@"user.do?action=getTargetStatus2" Params:@{@"supervisorUid":Dict[@"uid" ],@"executeUid":executeUid} Complete:^(id responseObj) {
                            
                            //小目标
                            VC.Small_NoFinish = responseObj[@"t"][@"notFinished"];
                            VC.Small_Finish = responseObj[@"t"][@"finished"];
                            
                            [self RefreshDataWithUrlStr:@"user.do?action=getBigTargetStatus2" Params:@{@"supervisorUid":Dict[@"uid"],@"executeUid":executeUid,@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
                                
                                //大目标
                                VC.Big_NoFinish = responseObj[@"t"][@"notFinished"];
                                VC.Big_Finish = responseObj[@"t"][@"finished"];
                                VC.HNCreditGoldControllerAccoutType = HNCreditGoldControllerAccoutTypeLaoShi;
                                
                                    
                                    VC.uid = Dict[@"uid"];
                                    VC.executeUid =executeUid;
                                    [_VC.navigationController pushViewController:VC animated:YES];
                                }];
                            }];
                        }];
                    }];
                    break;
                }
            }
        }];
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:SelectView];
    }];
    
}




+ (NSArray *)GetJiaZhangInfo
{
    return @[@"自己",@"孩子",@"老师"];
}

- (void)RefreshDataWithUrlStr:(NSString *)UrlStr Params:(NSDictionary *)Params Complete:(void(^)(id))Complete
{
    [MBProgressHUD showHUD];
    [XDHttpTool get:BaseURL(UrlStr) authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Params success:^(id responseObj) {
        
        
        Complete (responseObj);
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
    }];
}
@end
